define(["bbcare"], function ($bb) {
    var event = {}, vm = null, URL = $bb.baseUrl('followup')


    event.init = function () {
        if (vm) vm = null
        vm = createVue()
    }

    function createVue() {
        return new Vue({
            el: '#root',
            data: {
                search: {
                    name: '',
                    phone: '',
                    status: '在院',
                    wardNo: '',
                    roomNo: '',
                    loading: false,
                    date: null
                },
                dialog: {
                    show: false,
                    data: [],
                    loading: false,
                    size: 2,
                    total: 0,
                    current: 1
                },
                table: {
                    data: [],
                    loading: false,
                    total: 0,
                    current: 1,
                    size: 10,
                },
                loading: false
            },
            created: function () {
                this.getTableList()
            },
            methods: {
                handleSearch: function () {
                    this.getTableList()
                },
                handleShowDialog: function(row) {
                    this.dialog.show = true
                    var that = this
                    var params = {
                        currentPage: this.dialog.current,
                        pageSize: this.dialog.size,
                        userName: row.userName,
                        phone: row.phone,
                        inPatientNo: row.inPatientNo,
                        hospitalBedNo: row.hospitalBedNo,
                        userId: row.userId,
                        type: 2
                    }
                    $bb.ajax({
                        url: URL + 'followup/milkPreparationRecord/listPage',
                        data: JSON.stringify(params),
                        success: function (res) {
                            if (res.success) {
                                that.dialog.data = res.data.data
                                that.dialog.total = res.data.count
                            } else {
                                that.$message.error(res.msg)
                            }
                        }
                    })
                },
                handleCancel: function () {
                    this.dialog.show = false
                },
                /**
                 * 改变当前页大小
                 * @param {*} num 
                 */
                handleSizeChange: function (num) {
                    this.table.size = num;
                    this.getTableList()
                },
                /**
                 * 改变页面大小
                 */
                handleChangePage: function (num) {
                    this.table.current = num
                    this.getTableList()
                },
                handleChangeDetailPage: function (num) {
                    this.dialog.current = num
                    
                },
                handleClickRow: function (row) {
                    row.checked = !row.checked
                    this.$refs.data.toggleRowSelection(row)
                    this.$forceUpdate()
                },
                /**
                 * 获取列表数据
                 */
                getTableList: function () {
                    this.table.loading = true
                    var that = this
                    var params = {
                        currentPage: this.table.current,
                        pageSize: this.table.size,
                        userName: this.search.name,
                        phone: this.search.phone,
                        inpatientNo: '',
                        hospitalBedNo: this.search.bedNo,
                        inPatientNo: this.search.hospitalNo,
                        hospitalWard: this.search.wardNo,
                        sendTime: this.getFormatTime(this.search.date)
                    }
                    $bb.ajax({
                        url: URL + 'followup/milkPreparationRecord/sendHistoryPage',
                        data: JSON.stringify(params),
                        success: function (res) {
                            if (res.success) {
                                res.data.data.forEach(function (item, index) {
                                    item.No = (that.table.current - 1) * that.table.size + index + 1
                                    item.checked = false
                                })
                                that.table.data = res.data.data
                                that.table.total = res.data.count
                            } else {
                                that.$message.error('请求列表数据出错')
                            }
                            that.table.loading = false
                        }
                    })
                },
                getFormatTime: function (date) {
                    if (!date) return ''
                    var year = date.getFullYear(),
                       month = date.getMonth() + 1,
                         day = date.getDate()
                    return year + '-' + month + '-' + day
                }
            }
        })
    }

    return event
})